local thbattle__huoqinge_xiexian = fk.CreateSkill {

  name = "thbattle__huoqinge_xiexian",

  tags = { Skill.Compulsory, },

}



thbattle__huoqinge_xiexian:addEffect("active", {
  anim_type = "control",
  prompt = "#thbattle__huoqinge_xiexian-active",
  card_filter = Util.FalseFunc,
  target_filter =function (self, player, to_select, selected, selected_cards)
    return #selected == 0 and to_select ~= player
  end,
  target_num = 1,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    room:setPlayerMark(player, "thbattle__huoqinge_xiexian_target", target.id)
    local general_info = { player.general, player.deputyGeneral }
    local tar_player = target
    player.general = tar_player.general
    player.deputyGeneral = tar_player.deputyGeneral
    room:broadcastProperty(player, "general")
    room:broadcastProperty(player, "deputyGeneral")
    local _, ret = room:askForUseActiveSkill(player, "thbattle__huoqinge_xiexian_use",
      "#thbattle__huoqinge_xiexian-use:" .. target.id, true)
    room:setPlayerMark(player, "thbattle__huoqinge_xiexian_target", 0)
    player.general = general_info[1]
    player.deputyGeneral = general_info[2]
    room:broadcastProperty(player, "general")
    room:broadcastProperty(player, "deputyGeneral")
    if ret then
      local use_card = Fk:getCardById(ret.cards[1])
      local slash = Fk:cloneCard("slash")
      room:doBroadcastNotify("ShowToast", "以【邪仙】使用的【杀】会计入你的【杀】使用次数")
      if use_card.trueName == "slash" and
          THB_Utility.withinTimesLimit(player, Player.HistoryPhase, slash, "slash", room:getPlayerById(ret.targets[1]), slash.skill, 1) then
        room:addCardMark(use_card, "@@thbattle__huoqinge_xiexian")
        player:addCardUseHistory("slash", 1)
        room:useCard({
          from = target,
          tos = table.map(ret.targets, Util.Id2PlayerMapper),
          card = use_card,
        })
      elseif use_card.trueName == "slash" then
        room:doBroadcastNotify("ShowToast", "本次使用失败")
      else
        room:useCard({
          from=target,
          tos=table.map(ret.targets, Util.Id2PlayerMapper),
          card=Fk:getCardById(ret.cards[1]),
          extraUse=true
        })
      end
    end
  end,
})

thbattle__huoqinge_xiexian:addEffect(fk.TargetConfirmed, {
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(thbattle__huoqinge_xiexian.name) and data.card and
        data.card:getMark("@@thbattle__huoqinge_xiexian") > 0 and data.card.multiple_targets == true
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, thbattle__huoqinge_xiexian.name, nil,
      "#thbattle__huoqinge_xiexian_targetmod-use:::" .. data.card:toLogString())
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    data:cancelTarget(player)
  end,
})

thbattle__huoqinge_xiexian:addEffect(fk.BeforeCardUseEffect, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(thbattle__huoqinge_xiexian.name) and data.card and
        data.card:getMark("@@thbattle__huoqinge_xiexian") > 0 and data.card.multiple_targets == true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local tos_sort, temp = {}, target
    if table.find(data.tos, function(e)
          return e == target
        end) then
      table.insert(tos_sort, target)
    end
    while true do
      temp = temp.next
      if temp == target then
        break
      end
      if table.find(data.tos, function(e)
            return e == temp
          end) then
        table.insert(tos_sort, temp)
      end
    end
    
    data.tos = tos_sort
  end,
})

return thbattle__huoqinge_xiexian